﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UserService.Entity;
using UserService.IRepository;

namespace UserService.Repository
{
    public class UserManager<TUser> : IDisposable where TUser : class
    {
        protected internal IUserStore<TUser> Store { get; set; }

        public UserManager(IUserStore<TUser> store)
        {
            if (store == null)
            {
                throw new ArgumentNullException("store");
            }
            Store = store;
        }

        public virtual bool AddUser(TUser obj)
        {

            return Store.CreateRow(obj);
        }

        public virtual TUser FindByIdToUser(String userId)
        {
            return Store.FindByIdRow(userId);
        }

        public virtual ICollection<TUser> FindByIdToUser(string userName, string password)
        {
            return Store.FindByIdRow(userName, password);
        }

        /// <summary>
        ///     Find a user by user name
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public virtual TUser FindBySql(string sSql)
        {
            if (sSql == null)
            {
                throw new ArgumentNullException("userName");
            }
            return Store.FindBySql(sSql);
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                Store.Dispose();
            }
        }
    }
}
